package com.scud.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.scud.model.system.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

@Slf4j
public class SecurityUtil {
    /**
     * 通过Spring Security上下文获取用户信息
     *
     * @return
     */
    public static User getUser() {
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principal instanceof String) {
                String userJson = principal.toString();
                return JSONObject.parseObject(userJson, User.class);
            }
        } catch (Exception e) {
            log.error("获取当前用户信息错误：{}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}
